package com.processmining.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.processmining.dao.EventLogMapper;
import com.processmining.dao.MergeEventLogMapper;
import com.processmining.dao.UserMapper;
import com.processmining.entity.EventLog;
import com.processmining.entity.MergeEventLog;
import com.processmining.entity.User;
import com.processmining.service.IMergeEventLogService;
import com.processmining.util.fs.HDFSOperator;

@Service
public class MergeEventLogServiceImpl implements IMergeEventLogService {
	
	
	private final String EVENTLOG_PATH_PREFIX = "/processmining/eventlog/";
	
	private static Logger logger = LogManager.getLogger(MergeEventLogServiceImpl.class.getName());
	
	
	@Autowired
	private MergeEventLogMapper melMapper;
	
	@Autowired
	private EventLogMapper eventLogDao;
	
	@Autowired 
	private HDFSOperator hdfs;
	
	@Autowired
	private UserMapper userDao;

	public int insert(MergeEventLog mel) {
		return melMapper.insertSelective(mel);
	}

	public PageInfo<MergeEventLog> listAllMergeEventLog(Integer pageNum,Integer pageSize,Integer uid) {
		PageHelper.startPage(pageNum,pageSize);
		List<MergeEventLog> logList = melMapper.listAllMergeEventLog();
		uid = 1;
		List<MergeEventLog> logs = new ArrayList<MergeEventLog>();
		for(MergeEventLog log : logList){
			
			//System.out.println(log.toString());

			EventLog eLog = eventLogDao.selectByPrimaryKey(log.getTargeteventlogid());
			//System.out.println(log.getTargeteventlogid());
			//System.out.println(eLog==null);
			if(eLog.getCreatorid()!=uid&&!eLog.getIsshared())
				continue;
			
			
			// 创建者
			User user = userDao.selectByPrimaryKey(eLog.getCreatorid());
			log.setCreator(user.getNickname());
			
			// 时间
			DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			eLog.setCreateTime(format);
			log.setCreateTime(eLog.getCreateTime());
			
			logs.add(log);
			
		}
		PageInfo<MergeEventLog> logListWithPage = new PageInfo<MergeEventLog>(logs);
	
		return logListWithPage;
	}

	public PageInfo<MergeEventLog> search(Integer pageNum,Integer pageSize,Integer uid,String searchInfo) {
		PageHelper.startPage(pageNum,pageSize);
		List<MergeEventLog> logList = melMapper.listAllMergeEventLog();
		
		List<MergeEventLog> logs = new ArrayList<MergeEventLog>();
		for(MergeEventLog log : logList){
			
			System.out.println(log.toString());

			EventLog eLog = eventLogDao.selectByPrimaryKey(log.getTargeteventlogid());
			if(eLog.getCreatorid()!=uid&&!eLog.getIsshared())
				continue;
			
			
			// 创建者
			User user = userDao.selectByPrimaryKey(eLog.getCreatorid());
			log.setCreator(user.getNickname());
			
			// 时间
			DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			eLog.setCreateTime(format);
			log.setCreateTime(eLog.getCreateTime());
			
			logs.add(log);
			
		}
		List<MergeEventLog> logs2 = new ArrayList<MergeEventLog>();
		for(int i=0;i<logs.size();i++){
			MergeEventLog m=logs.get(i);
			if(m.getTargetname().contains(searchInfo)){
				System.out.print("["+m.getTargetname()+"contains"+searchInfo+"]");
				logs2.add(m);
			}
		}
		
		PageInfo<MergeEventLog> logListWithPage = new PageInfo<MergeEventLog>(logs2);
	
		return logListWithPage;
	}
	
	
	@Override
	public boolean deleteBatch(List<Integer> logIds) throws Exception {
		boolean ret = false;
		for(Integer id : logIds){
			MergeEventLog mlog = melMapper.selectByPrimaryKey(id);
			if(mlog!=null){
				EventLog elog = eventLogDao.selectByPrimaryKey(mlog.getTargeteventlogid());
				if(elog!=null){
					hdfs.deleteFile(EVENTLOG_PATH_PREFIX+elog.getHdfsid());
					eventLogDao.deleteByPrimaryKey(elog.getId());
				}
				melMapper.deleteByPrimaryKey(id);
			}
		}
		return ret;

	}

}
